"""
@File    :   models.py
@Time    :   2024/02/07 10:45:57
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   租户数据模型
"""

from datetime import datetime

from sqlalchemy import BigInteger, Boolean, Column, DateTime, Integer, String

from src.models.models import OrmBase


class Tenant(OrmBase):
    """租户模型
    使用COLUMN 字段隔离模式
    直接不使用数据库隔离模式(因为会涉及到分布式事务)
    """

    __tablename__ = "system_tenant"
    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    name = Column(String(200), nullable=False, unique=True, comment="租户名")
    admin_user_id = Column(
        BigInteger(), default=0, nullable=False, comment="管理员用户ID"
    )
    admin_username = Column(
        String(20), default="", nullable=False, comment="管理员用户名"
    )
    admin_remark = Column(
        String(300), default="", nullable=False, comment="租户默认管理员的备注"
    )
    bind_domain = Column(
        String(200),
        default="",
        nullable=False,
        unique=True,
        index=True,
        comment="绑定域名",
    )
    quota = Column(Integer(), nullable=False, index=True, comment="额度")
    status = Column(
        Boolean(), default=True, nullable=False, comment="状态"
    )  # (True正常 False停用)
    show_order = Column(
        Integer(), nullable=False, index=True, default=1, comment="显示顺序"
    )
    remark = Column(String(300), default="", nullable=False, comment="备注")
    is_system = Column(
        Boolean(), default=False, nullable=False, comment="是否系统级别租户"
    )
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    update_datetime = Column(
        DateTime(),
        nullable=False,
        default=datetime.now,
        onupdate=datetime.now,
        index=True,
        comment="修改时间",
    )
    # 创建者用户名(级联删除)
    creator_username = Column(
        String(20), default="", nullable=False, comment="创建者用户名"
    )
    # 修改者用户名(级联删除)
    updater_username = Column(
        String(20), default="", nullable=False, comment="修改者用户名"
    )
    expiration_datetime = Column(
        DateTime(), nullable=False, index=True, comment="过期时间"
    )
    tenant_package_id = Column(
        Integer(), nullable=False, index=True, comment="租户套餐ID"
    )
